//
//  RCEditUserInfoVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-11-22.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCEditUserInfoVC.h"
#import "IManager+ModulePersonCenter.h"
#import "RCEditAllCityVC.h"
#import "NSString+NSString_Ex_.h"
#import "RCEditSexVC.h"
#import "AppDelegate.h"

@interface RCEditUserInfoVC ()<IManagerDelegate,RCEditCityProtocol,RCEditSexProtocol>{
    NSString *_uName;
    NSString *_cityName;
    NSString *_cityID;
    BOOL _isMan;
    
    UITextField *_nameField;
    UILabel *liveLabel;
    UILabel *sexLabel;
    
    int64_t reqID;
}

@end

@implementation RCEditUserInfoVC
//@synthesize model = _model;

- (id) initWithNickName:(NSString *)userName liveCity:(NSString *)cityName liveCityID:(NSString *)cityID isMan:(BOOL)sexType
{
    self = [super init];
    if (self) {
        _uName = [userName copy];
        _cityName = [cityName copy];
        _cityID = [cityID copy];
        _isMan = sexType;
    }
    return self;
}


- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:reqID];

    [_uName release];
    [_cityName release];
    [_cityID release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabelText = @"修改个人信息";
    self.navigationItem.titleView = [self createTitleView];
    
    UIImage *IMG = [UIImage imageNamed:@"editInfo_bg"];
    
    //昵称
    UIImageView *nickBG = [[UIImageView alloc] initWithFrame:CGRectMake(16, 23, 287, 39)];
    nickBG.image = IMG;
    [self.viewContainer addSubview:nickBG];
    [nickBG release];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 25, 50, 35)];
    [self.viewContainer addSubview:nameLabel];
    nameLabel.text = @"昵称";
    [nameLabel release];
    
    _nameField = [[UITextField alloc] initWithFrame:CGRectMake(76, 30, 220, 25)];
    [self.viewContainer addSubview:_nameField];
    _nameField.text = _uName;
    _nameField.clearButtonMode = UITextFieldViewModeAlways;
    [_nameField becomeFirstResponder];
    [_nameField release];
    
    //常居地
    UIImageView *liveBG = [[UIImageView alloc] initWithFrame:CGRectMake(16, 85, 287, 39)];
    liveBG.image = IMG;
    [self.viewContainer addSubview:liveBG];
    [liveBG release];
    
    liveLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 88, 200, 30)];
    [self.viewContainer addSubview:liveLabel];
//    [liveLabel setBackgroundColor:[UIColor yellowColor]];
    liveLabel.text = [NSString stringWithFormat:@"常居地  %@",_cityName];
    [liveLabel release];
    
    UIImage *indicator = [UIImage imageNamed:@"editInfo_indicator"];
    UIImageView *inc = [[UIImageView alloc] initWithFrame:CGRectMake(280, 98, 8, 10)];
    inc.image = indicator;
    [self.viewContainer addSubview:inc];
    [inc release];
    
    UIButton *changePlaceBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, 80, 287, 39)];
    [self.viewContainer addSubview:changePlaceBtn];
    [changePlaceBtn addTarget:self action:@selector(changePlaceAction) forControlEvents:UIControlEventTouchUpInside];
    [changePlaceBtn release];
    
    //性别
    UIImageView *sexBG = [[UIImageView alloc] initWithFrame:CGRectMake(16, 143, 287, 39)];
    sexBG.image = IMG;
    [self.viewContainer addSubview:sexBG];
    [sexBG release];
    
    sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 145, 250, 30)];
    [self.viewContainer addSubview:sexLabel];
    if (_isMan) {
        sexLabel.text = @"性别        男";
    }else{
        sexLabel.text = @"性别        女";
    }
    [sexLabel release];
    
    UIImageView *incSex = [[UIImageView alloc] initWithFrame:CGRectMake(280, 95+60, 8, 10)];
    incSex.image = indicator;
    [self.viewContainer addSubview:incSex];
    [incSex release];
    
    UIButton *changeSexBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, 140, 287, 39)];
    [self.viewContainer addSubview:changeSexBtn];
    [changeSexBtn addTarget:self action:@selector(changeSexAction) forControlEvents:UIControlEventTouchUpInside];
    [changeSexBtn release];
    
    
     UIButton *_rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0) {
        _rightButton.frame = CGRectMake(10, 0, 44, 44);
    }
#define kRightButtonImageFile    @"image_merchant_commit_wrong_rightButton.png"

    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
                  forState:UIControlStateNormal];
    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
                  forState:UIControlStateHighlighted];
    [_rightButton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:_rightButton];
    
//    //提交按钮
//    UIButton *rightBtn = [[[UIButton alloc] init] autorelease];
//    [rightBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"bg_follow_btn"] forState:UIControlStateNormal];
////    rightBtn setBackgroundImage:(UIImage *) forState:<#(UIControlState)#>
//    [rightBtn setTitle:@"提交" forState:UIControlStateNormal];
//    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:rightBtn];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) submit
{
    if ([NSString stringIsNullOrEmpty:_cityID]) {
        [RCAppDelegate alertWithCustomizeString:@"请重新选择城市"];
        return;
    }
  
    if (![NSString stringIsNullOrEmpty:_nameField.text]) {
        NSInteger sexInt = 1;
        if (_isMan) {
            sexInt = 1; 
        }else{
            sexInt = 2;
        }
        [RCAppDelegate alertWithCustomizeString:@"提交"];

        [[IManager sharedInstance] cancelFetchDataRequestByRequestID:reqID];
        reqID = [[IManager sharedInstance] requestEditUserInfo:_nameField.text withSex:sexInt withLiveCity:[_cityID intValue] WithDelegte:self];
    }else{
        [RCAppDelegate alertWithCustomizeString:@"昵称不能为空"];

    }
}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"提交失败"];

}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"提交成功"];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void) changePlaceAction
{
    RCEditAllCityVC *vc = [[RCEditAllCityVC alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


- (void) changeSexAction
{
    
    RCEditSexVC *vc = [[RCEditSexVC alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

- (void) changeCityTo:(NSString *)cityName withCityID:(NSString *)cityID
{
    _cityName = [cityName copy];
    _cityID = [cityID copy];
    
    liveLabel.text = [NSString stringWithFormat:@"常居地  %@",_cityName];

}

- (void) changeSexToMan:(BOOL)isMan
{
    _isMan = isMan;
    if (_isMan) {
        sexLabel.text = @"性别        男";
    }else{
        sexLabel.text = @"性别        女";
    }
}
@end
